package car.recording.system.application;

import java.util.ArrayList;

import com.recording.system.events.RecordingSystemEvent;
import com.recording.system.sensor.AbstractSensor;
import com.recording.system.signalsource.SignalSource;

/** 
 * main class for car recording system
 * @author bpkx1031
 *
 */
public class CarRecordingSystem {
	/**
	 * hold all signal sources for recording system
	 */
	private ArrayList<SignalSource> signalSources = new ArrayList<SignalSource>();
	/**
	 * hold all sensors for recording system
	 */
	private ArrayList<AbstractSensor> sensors = new ArrayList<AbstractSensor>();
	/**
	 * @return the signalSource
	 */
	public SignalSource getSignalSource(int index) {
		return signalSources.get(index);
	}
	public SignalSource getSignalSourceById(int id)
	{
		for (SignalSource signalSource: signalSources) {
			if (signalSource.getId() == id)
				return signalSource;
		}
		return null;
	}
	/**
	 * 
	 * @return signal sources count
	 */
	public int getSignalSourcesCount()
	{
		return signalSources.size();
	}
	/**
	 * @return the sensor
	 */
	public AbstractSensor getSensor(int index) {
		return sensors.get(index);
	}
	/**
	 * 
	 * @return sensors count
	 */
	public int getSensorsCount()
	{
		return sensors.size();
	}
	/**
	 * @param sensor - new sensor for system 
	 */
	public void addSensor(AbstractSensor sensor)
	{
		SignalSource signalSource = new SignalSource(sensor.getSourceId());
		signalSources.add(signalSource);
		sensors.add(sensor);
	}
	public ArrayList<RecordingSystemEvent> readAllData()
	{
		ArrayList<RecordingSystemEvent> res = new ArrayList<RecordingSystemEvent>();
		ArrayList<RecordingSystemEvent> tmp = new ArrayList<RecordingSystemEvent>();
		for (AbstractSensor sensor : sensors)
		{
			tmp.clear();
			SignalSource signalSource = getSignalSourceById(sensor.getSourceId());
			sensor.readSignalSource(signalSource,tmp);
			res.addAll(tmp);
		}
		return res;
	}
	
}
